import { TagSharp } from "@mui/icons-material";
import dayjs from "dayjs";
import { parseToRecordDayList } from "redux/reducers/recordList";

const parseCategoryByTag = (records: RecordType[], type: number) => {
  const recordsDay = parseToRecordDayList(records);
  const curDay = recordsDay.find((record) => {
    return record.currentDate === dayjs().format("M月DD日");
  });
  //筛选出当天的
  const optdata = curDay?.children
    .map((child) => {
      return {
        value: type === 0 ? child.output : child.input,
        name: child.tags[0].tagName,
      };
    })
    .filter((item) => item.value !== 0);
  type tagHash = {
    [index: string]: { value: number; name: string }[];
  };
  const tagHash: tagHash = {};
  optdata?.map((item) => {
    if (tagHash[item.name]) {
      tagHash[item.name].push(item);
    } else {
      tagHash[item.name] = [item];
    }
    return item;
  });
  const newValue: { value: number; name: string }[] = [];
  let key: keyof tagHash;
  for (key in tagHash) {
    let total = 0;
    tagHash[key].map((item) => {
      total += item.value;
    });
    newValue.push({ name: key, value: total });
  }
  return newValue;
};
export { parseCategoryByTag };
